package com.mlnx.work.pojo.vo;

import com.mlnx.work.enums.ProjectStatusEnum;
import com.mlnx.work.enums.ProjectTypeEnum;
import com.mlnx.work.utils.DateUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Data
@ApiModel("项目展示返回参数类")
public class ProjectTaskVO {
    @ApiModelProperty("项目层级ID")
    private Integer dirId;

    @ApiModelProperty("自动编号")
    private String itemNo;

    @ApiModelProperty("项目层级名称")
    private String dirName;

    @ApiModelProperty("最初计划开始时间")
    private Long originStartTime;

    @ApiModelProperty("实际开始时间")
    private Long startTime;

    @ApiModelProperty("最初计划结束时间")
    private Long originEndTime;

    @ApiModelProperty("实际完成时间")
    private Long completedTime;

    @ApiModelProperty("项目状态")
    private ProjectStatusEnum status;

    @ApiModelProperty("项目类型")
    private ProjectTypeEnum type;

    @ApiModelProperty("逾期计划时间")
    private Long overdueTime;

    @ApiModelProperty("逾期比")
    private Float overdueRate;

    @ApiModelProperty("逾期次数")
    private Integer overdueTimes;

    @ApiModelProperty("交付成果")
    private String achievement;

    @ApiModelProperty(value = "风险项")
    private String risk;

    @ApiModelProperty(value = "标签")
    private String tags;

    @ApiModelProperty("里程碑")
    private Boolean milepost;

    @ApiModelProperty("负责人")
    private String manager;

    @ApiModelProperty("任务")
    private List<TaskVO> taskVOS;

    @ApiModelProperty("子层级")
    private List<ProjectTaskVO> childProjectTaskVO;

    @ApiModelProperty("通用子级")
    private List<Map<String, Object>> childrenMap;

    public String getStatus() {
        if (ProjectStatusEnum.ING.equals(status))
            if (DateUtil.dateCompare(originEndTime, System.currentTimeMillis()) < 0)
                status = ProjectStatusEnum.OVERDUE;
            else if (!risk.trim().isEmpty())
                status = ProjectStatusEnum.RISK;

        return status.getStatus();
    }

    public String getType(){
        if(type.getValue() == 0)
            return "";
        return type.getName();
    }

    public Float getOverdueRate() {
        if (overdueTime == null)
            return 0f;

        float f1 = DateUtil.dayBetween(overdueTime, originStartTime) + 1;
        float f2 = DateUtil.dayBetween(originEndTime, originStartTime) + 1;
        return f1 / f2;
    }

    public Map<String, Object> toCommonMap() {
        Map<String, Object> map = new HashMap<>();
        map.put("id", dirId);
        map.put("itemNo", itemNo);
        map.put("dirName", dirName);
        map.put("originStartTime", originStartTime);
        map.put("startTime", startTime);
        map.put("originEndTime", originEndTime);
        map.put("completedTime", completedTime);
        map.put("status", status.getStatus());
        map.put("overdueTime", overdueTime);
        map.put("overdueRate", overdueRate);
        map.put("overdueTimes", overdueTimes);
        map.put("achievement", achievement);
        map.put("risk", risk);
        map.put("tags", tags);
        map.put("milepost", milepost);
        map.put("workHour", null);
        map.put("manager", manager);
        if (childProjectTaskVO != null) {
            List<Map<String, Object>> list = new ArrayList<>();
            for (ProjectTaskVO projectTaskVO : childProjectTaskVO) {
                list.add(projectTaskVO.toCommonMap());
            }
            map.put("childrenMap", list);
        }

        return map;
    }

    public List<Map<String, Object>> getChildrenMap() {

        List<Map<String, Object>> list = new ArrayList<>();
        if (childProjectTaskVO != null) {
            for (ProjectTaskVO projectTaskVO : childProjectTaskVO) {
                list.add(projectTaskVO.toCommonMap());
            }
        }

        if (taskVOS != null) {
            for (TaskVO taskVO : taskVOS) {
                list.add(taskVO.toCommonMap());
            }
        }
        return list;
    }
}
